package view;

import controller.AccountValidation;
import controller.OperationFile;
import model.FileRules;
import model.ScanRead;
import sun.applet.AppletResourceLoader;

import java.io.IOException;

/**
 * description: 管理员界面的类
 * author: 时间
 * time: 2022-01-11-15:07
 */
public class AdministratorView extends ViewRun implements Rules{
    @Override
    public void views() throws IOException {
        System.out.println("***************************管理员界面***************************");
        System.out.println("1-添加\t\t2-修改\t\t3-删除\t\t4-查看\t\t5-退出登录");
        System.out.println("***************************************************************");
        System.out.print("请选择：");
        char chr = ScanRead.readLogin();
        switch (chr){
            case '1':
                addClass();
                break;
            case '2':
                modifyClass();
                break;
            case '3':
                deleteClass();
                break;
            case '4':
                listClass();
                break;
            case '5':
                exit();
                break;
        }
    }

    // 添加教室操作（注意把这种注释改为文档注释），
    // 管理员仅需要输入教室号，教室类型，备注。（可容纳人数与教室状态调用ClassToUser接口中的全局常量）
    // 将需要添加的内容a以(A101-智慧教室-30~40-空闲-无)形式传给writeClassroomFile(a)方法
    private void addClass(){
        OperationFile operationFile = new OperationFile();
        flag1 = true;
        while (flag1){
            System.out.print("请输入教室号(-1退出)：");
            String number = ScanRead.readClassroom();
            if (number.equals("-1")){
                break;
            }
            if (AccountValidation.classNumExist(number, pattern1)){
                System.out.println("该教室已经存在，请重新输入，");
                continue;
            }
            if (number.length() != 4 && (number.charAt(0) != 'A' && number.charAt(0) != 'B' && number.charAt(0) != 'C')) {
                System.out.print("教室号的格式为(A101),请重新输入，");
                continue;
            }
            System.out.print("请输入教室说明：");
            String note = ScanRead.readClassroom();
            if (number.charAt(0) == 'A'){
                operationFile.writeClassroomFile(number + "-" + CLASS1 + "-" + COUNT1
                        + "-" + STATE1 + "-" + note);
            }
            else if (number.charAt(0) == 'B'){
                operationFile.writeClassroomFile(number + "-" + CLASS2 + "-" + COUNT2
                        + "-" + STATE1 + "-" + note);
            }
            else {
                operationFile.writeClassroomFile(number + "-" + CLASS3 + "-" + COUNT3
                        + "-" + STATE1 + "-" + note);
            }
            System.out.println("添加成功");
            ScanRead.readReturn();
            break;
        }
    }

    // 修改教室操作（同上）将需要修改的内容传给changeClassroomFile(a, b)方法，传入两个参数
    // a:教室号, b:(A101-智慧教室-30~40-空闲-无)这种形式的内容
    private void modifyClass() throws IOException {
        OperationFile operationFile = new OperationFile();
        while (true){
            System.out.print("请输入教室号(-1退出)：");
            String number = ScanRead.readClassroom();
            if (number.equals("-1")) {
                break;
            }
            if (number.length() != 4 && (number.charAt(0) != 'A' || number.charAt(0) != 'B' || number.charAt(0) != 'C')) {
                System.out.print("教室号的格式为(A101),请重新输入，");
                continue;
            }
            if (AccountValidation.classNumExist(number, pattern1)){
                System.out.print("请输入教室状态(空闲，已预约，排课，停止使用)：");
                String state = ScanRead.readClassroom();
                if(state.equals("空闲") || state.equals("排课") || state.equals("停止使用")){
                    System.out.print("请输入教室说明：");
                    String note = ScanRead.readClassroom();
                    if (number.charAt(0) == 'A'){
                        operationFile.changeClassroomFile(number, number + "-" + CLASS1 + "-" + COUNT1
                                + "-" + state + "-" + note);
                    }
                    else if (number.charAt(0) == 'B'){
                        operationFile.changeClassroomFile(number, number + "-" + CLASS2 + "-" + COUNT2
                                + "-" + state + "-" + note);
                    }
                    else {
                        operationFile.changeClassroomFile(number, number + "-" + CLASS3 + "-" + COUNT3
                                + "-" + state + "-" + note);
                    }
                    System.out.println("修改成功");
                    ScanRead.readReturn();
                    break;
                }
                System.out.println("输入有误(空闲，已预约，排课，停止使用),请重新输入");
            }
            System.out.println("该教室不存在，请重新输入，");
        }
    }

    // 删除教室操作，将需要删除的教室号传入deleteClassroomFile(a), a:教室号
    // 删除之前需要调用classNumExist(a, b),a:教室号， b:正则匹配规则pattern2
    private void deleteClass() throws IOException {
        OperationFile operationFile = new OperationFile();
        while (true){
            System.out.print("请输入教室号：");
            String number = ScanRead.readClassroom();
            if (number.equals("-1")) {
                break;
            }
            if (number.length() != 4 && (number.charAt(0) != 'A' || number.charAt(0) != 'B' || number.charAt(0) != 'C')) {
                System.out.print("教室号的格式为(A101),请重新输入，");
                continue;
            }
            boolean classExist = AccountValidation.classNumExist(number, pattern1);
            if (classExist){
                System.out.print("确定删除(y/n)：");
                char chr = ScanRead.readSelection();
                if (chr == 'Y'){
                    operationFile.deleteClassroomFile(number, FileRules.pathCl);
                    System.out.println("删除成功");
                    ScanRead.readReturn();
                    break;
                }
            }
            System.out.println("该教室不存在，请重新输入");
        }
    }

    // 查看，将flags置8
    private void listClass(){
        flags = 8;
    }

    // 退出登录（将flags置0）
    @Override
    public void exit() {
        System.out.print("确定退出(y/n):");
        char chr = ScanRead.readSelection();
        if (chr == 'Y'){
            flags = 0;
        }else{
            flags = 2;
        }

    }
}
